﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MefTZAddin.interop.types;

namespace MefTZAddin.interop
{
    public static class SystemTimeInterop
    {
        public static SystemTime ToSystemTime(this DateTime dt)
        {
            SystemTime st = new SystemTime();
            st.year = (short)0; // Most timezone changes are relative each year
            st.month = (short)dt.Month;

            int weekdayOfMonth = 1; // e.g. third Monday = 3...
            for (int dd = dt.Day; dd > 7; dd -= 7)
                weekdayOfMonth++;

            st.day = (short)weekdayOfMonth;
            st.dayOfWeek = (short)dt.DayOfWeek;
            st.hour = (short)dt.Hour;
            st.minute = (short)dt.Minute;
            st.second = (short)dt.Second;
            st.milliseconds = (short)dt.Millisecond;

            return st;
        }

        public static SystemTime ToSystemTime(this TimeZoneInfo.TransitionTime tt)
        {
            SystemTime st = new SystemTime();

            st.year = (short)0;
            st.month = (short)tt.Month; // 1 = January

            int weekdayOfMonth = 1; // e.g. third Monday = 3...
            for (int dd = tt.Day; dd > 7; dd -= 7)
                weekdayOfMonth++;

            st.day = (short)weekdayOfMonth;
            st.dayOfWeek = (short)tt.DayOfWeek; // 0 = Sunday...

            st.hour = (short)tt.TimeOfDay.Hour;
            st.minute = (short)tt.TimeOfDay.Minute;
            st.second = (short)tt.TimeOfDay.Second;
            st.milliseconds = (short)tt.TimeOfDay.Millisecond;

            return st;
        }
    }
}
